/*
 * Copyright (c) 2008, Hugh Sanderson, http://gamehaxe.com/
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 */
 
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Rectangle;

import flash.display.DisplayObject;
import flash.geom.Matrix;
import flash.events.Event;
import flash.events.MouseEvent;

import flash.filters.BlurFilter;


class Filters extends Sprite
{
   static function main()
   {
      neash.Lib.Init("Filters",640,480,false,true); 
      neash.Lib.SetBackgroundColour(0xffffff); 

      new Filters();

      neash.Lib.SetFrameRate(1000);
      neash.Lib.ShowFPS();
      neash.Lib.Run(); 
   }

   var small_objs:Array<DisplayObject>;
   var big_objs:Array<DisplayObject>;

   public function new()
   {
      super();
      flash.Lib.current.addChild(this);

      small_objs = [];
      big_objs = [];

      for(q in 1...6)
         for(str in 1...4)
         {
             var obj = new Shape();
             var gfx = obj.graphics;
             gfx.beginFill(0xa00000);
             gfx.lineStyle(0,2);
             gfx.drawCircle(q*100,str*100-50,40);
             addChild(obj);

             small_objs.push(obj);
         }

      for(flags in 0...8)
      {
          var obj = new Shape();
          var gfx = obj.graphics;
          gfx.beginFill(0xa00000);
          gfx.lineStyle(0,2);
          gfx.drawCircle(flags*80+40,420,25);

          addChild(obj);
          big_objs.push(obj);
      }


      addEventListener(Event.ENTER_FRAME, OnEnterFrame);
      stage.addEventListener(MouseEvent.MOUSE_MOVE, OnMouseMove);
   }

   function OnMouseMove(e:MouseEvent)
   {
      var x = e.stageX - stage.stageWidth/2;
      var y = e.stageY - stage.stageHeight/2;
      SetupFilters(Math.sqrt(x*x+y*y)*0.05,Math.atan2(y,x)*180/Math.PI,10);
   }


   function SetupFilters(inDist:Float, inRad:Float,inBlur:Float)
   {
      var idx=0;
      for(q in 1...6)
         for(str in 1...4)
         {
             var glow = new flash.filters.GlowFilter(0xff0000,0.5,
                               inDist,inDist, str,q, false,(q&0x1)==0);
             var f = new Array<flash.filters.BitmapFilter>();
             f.push(glow);
             small_objs[idx++].filters = f;
         }
      idx = 0;
      for(flags in 0...8)
      {
          var filter = new flash.filters.DropShadowFilter(inDist,inRad,
                             0x000000,0.3, 10,10,1,2,
                            (flags&0x01) > 0 ,(flags&0x02) > 0 ,(flags&0x04) > 0 );
          var f = new Array<flash.filters.BitmapFilter>();
          f.push(filter);
          big_objs[idx++].filters = f;
      }
   }



   function OnEnterFrame(inEvent:Event)
   {
      // scene.x = Std.int(scene.x + 1) & 0x1ff;
   }
}
